#include<bits/stdc++.h>
using namespace std;
const int mod = 998244353;
int f[1011][1011],dep[1011],ff[1011],a[1011],siz[1011];
vector<int> v[1011],e[1011];
int dui[1011],idx;
bool onl[1011][1011];
bool b[1011];
int n,m;
void DFS1(int p)
{
	b[p] = 1;
	for(int i = 1;i <= n;i++)
		onl[p][i] = b[i];
	if(p) 
		dui[++idx] = p;
	siz[p] = 1;
	for(auto i:v[p])
		DFS1(i),siz[p] += siz[i];
	b[p] = 0;
}
void DFS2(int p)
{
	for(auto i:e[p])
		dep[i] = dep[ff[i] = p] + 1,DFS2(i);
}
int F(int i,int j)
{
	if(f[i][j])
		return f[i][j];
	if(i == j)
		return f[i][j] = dep[i];
	if(dep[i] < dep[j])
		swap(i,j);
	return f[i][j] = F(ff[i],j);
}
long long ans;
int ka,rt;
void DFS3(int p)
{
	ans += F(ka,a[p]) * siz[p] * (n - siz[rt]);
	for(auto i:v[p])
		DFS3(i);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0),cout.tie(0);
	freopen("tree.in","r",stdin);
	freopen("tree.out","w",stdout);
	cin >> n >> m;
	if(n > 1000 || m > 1000)
	{
		cout << 527664804;
		return 0;
	}
	for(int i = 1;i <= n;i++)
	{
		int f;
		cin >> f;
		v[f].push_back(i);
	}
	for(int i = 1;i <= m;i++)
	{
		int f;
		cin >> f;
		e[f].push_back(i);
	}
	DFS1(0);
	dep[0] = 0,DFS2(0);
	for(int i = 1;i <= n;i++)
		cin >> a[i];
	for(int i = 1;i < n;i++)
	{
		for(int j = i + 1;j <= n;j++)
		{
			int x = dui[i],y = dui[j],w = F(a[i],a[j]);
			if(!onl[y][x])
				ans += w * siz[x] * siz[y];
		}
	}
	for(int i = 1;i <= n;i++)
	{
		ka = a[i];
		for(auto x:v[i])
			DFS3(rt = x);
	}
	cout << ans % mod;
	return 0;
}
